﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Objects;
using System.Linq;

namespace DAL.DBManagers
{
    public class BaseManager<T> : IBaseManager<T> where T : class
    {
        protected DbContext _context;
        protected DbSet<T> _objectSet;

        public BaseManager()
            :this(new PortEntities())
        {

        }

        public BaseManager(PortEntities context)
        {
            _context = context;
            _objectSet = _context.Set<T>();
        }

        public IQueryable<T> Fetch()
        {
            return _objectSet;
        }

        public virtual IEnumerable<T> GetAll()
        {
            return Fetch().AsEnumerable().ToList();
        }

        public virtual List<T> GetAll(string sortBy)
        {
            List<T> prodList = Fetch().AsEnumerable().ToList();
            if (sortBy != "")
            {
                DAL.Models.GenericComparer<T> cmp = new DAL.Models.GenericComparer<T>(sortBy);
                prodList.Sort(cmp);
            }
            return prodList;
        }


        public IEnumerable<T> Find(Func<T, bool> predicate)
        {
            return _objectSet.Where<T>(predicate);
        }


        public T Single(Func<T, bool> predicate)
        {
            return _objectSet.Single<T>(predicate);
        }

        public T SingleOrDefault(Func<T, bool> predicate)
        {
            return _objectSet.SingleOrDefault<T>(predicate);
        }


        public T First(Func<T, bool> predicate)
        {
            return _objectSet.First<T>(predicate);
        }

        public T FirstOrDefault(Func<T, bool> predicate)
        {
            return _objectSet.FirstOrDefault<T>(predicate);
        }


        public void Delete(T entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }

            _objectSet.Remove(entity);
        }

        public void Delete(Func<T, bool> predicate)
        {
            IEnumerable<T> records = from x in _objectSet.Where<T>(predicate) select x;

            foreach (T record in records)
            {
                _objectSet.Remove(record);
            }
        }


        public void Add(T entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }

            _objectSet.Add(entity);
        }

        public void Attach(T entity)
        {
            _objectSet.Attach(entity);
        }


        public void SaveChanges()
        {
            _context.SaveChanges();
        }


        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (_context != null)
                {
                    _context.Dispose();
                    _context = null;
                }
            }
        }
    }
}
